class_name GameplayAbilitySpec
## Gameplay Ability Spec

##
var active: bool = true

##
var ability: GameplayAbility

##
var target: AbilitySystemComponent

##
var is_expired: bool = false


func _init(init_ability: GameplayAbility, init_target: AbilitySystemComponent) -> void:
	ability = init_ability
	target = init_target

	_on_target_set()


func on_target_set() -> void:
	_on_target_set()


func apply() -> void:
	if target:
		target.effect_manager.apply_effects(ability.ability_asset.granted_effects, ability.owner)
	
	_apply()


func on_update(delta: float) -> void:
	if not is_expired and ability.active and active:
		_on_update(delta)


func activate() -> void:
	if is_expired:
		return
	
	active = true
	
	for effect_asset: GameplayEffectAsset in ability.ability_asset.granted_effects:
		if effect_asset.duration_policy != GameplayEffectAsset.DurationPolicy.Instant:
			target.effect_manager.activate_effect(effect_asset)


func deactivate() -> void:
	if is_expired:
		return
	
	active = false

	for effect_asset: GameplayEffectAsset in ability.ability_asset.granted_effects:
		if effect_asset.duration_policy != GameplayEffectAsset.DurationPolicy.Instant:
			target.effect_manager.deactivate_effect(effect_asset)


func end_ability() -> void:
	if is_expired:
		return
	
	is_expired = true
	
	for effect_asset: GameplayEffectAsset in ability.ability_asset.granted_effects:
		if effect_asset.duration_policy != GameplayEffectAsset.DurationPolicy.Instant:
			target.effect_manager.remove_effect(effect_asset)
	
	_end_ability()


#region Need to override
func _on_target_set() -> void:
	pass
	

func _apply() -> void:
	pass


func _on_update(_delta: float) -> void:
	pass


func _end_ability() -> void:
	pass
#endregion
